$(document).ready(function() {
  var start,end;
  
  function changePrice(price,period,object)
  {
    //consider company or regular discount
    var company_discount = 0,discount = 0,paid=0,recently_paid=0;
    service_reveneue = 0;
    
    if(  $.isNumeric(  parseFloat($(object).parents(".view-booking-popup").find('#total_service_revenue').val())  )  )
        service_reveneue = parseFloat($(object).parents(".view-booking-popup").find('#total_service_revenue').val());
    
    if($(object).parents(".view-booking-popup").find('.discount-edit').text() != "")
    {
      company_discount = parseInt($(object).parents(".view-booking-popup").find('.discount-edit').text());
    }
    if($.isNumeric($(object).parents(".view-booking-popup").find('.get-discount-edit').val()))
    {
      discount = $(object).parents(".view-booking-popup").find('.get-discount-edit').val();
    }
    
    current = price*period;
    $(object).parents(".view-booking-popup").find('.total_price-edit').text(current+service_reveneue);
    current = current - (current*(discount/100));
    current = current - (current*(company_discount/100));
    current += service_reveneue;
    
    //update prices
    $(object).parents(".view-booking-popup").find('.final_price-edit').text(current);
    $(object).parents(".view-booking-popup").find('#final_price-edit').val(current);
    $(object).parents(".view-booking-popup").find('.credit-edit').text("-"+current);
    
    
    
    paid = parseFloat($(object).parents(".view-booking-popup").find('.what_paid').text());
    paid_back = parseFloat(  $(object).parents(".view-booking-popup").find('.paid_back').text()  );
    credit = $(object).parents(".view-booking-popup").find('.final_price-edit').text();
    new_credit = (   parseFloat(paid) - parseFloat(paid_back) - parseFloat(credit)  );

    $(object).parents(".view-booking-popup").find('.credit-edit').text(new_credit);
    if(new_credit > 0)
      clas = "green";
    else if(new_credit < 0)
      clas = "red";
    else
      clas = "black";
          
    $(object).parents(".view-booking-popup").find('.credit-edit').removeClass('red green');
    $(object).parents(".view-booking-popup").find('.credit-edit').addClass(clas);
  }
  
  $('.rent_type-edit').change(function(){
   
   if($(this).val() != 2)
    {
      //update apartment price
      if($.isNumeric($(this).parents(".view-booking-popup").find('#apartment-price-edit').text())  && $(this).val() == 0 )
      {
        var price = $(this).parents(".view-booking-popup").find('#daily_price-edit').val();
        $(this).parents(".view-booking-popup").find('#apartment-price-edit').text(price);   
      }
      else
      {
        var price =$(this).parents(".view-booking-popup").find('#weekly_price-edit').val();
        $(this).parents(".view-booking-popup").find('#apartment-price-edit').text(price);   
      }
      
        //update total price
      if($(this).parents(".view-booking-popup").find('.period-days-edit').val() != "" && $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  0)
      {
        period = $(this).parents(".view-booking-popup").find('.period-days-edit').val();
        changePrice(price,period,this)
      }
      else if ($(this).parents(".view-booking-popup").find('.period-days-edit').val() != "" && $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  1)
      {
        period = $(this).parents(".view-booking-popup").find('.period-days-edit').val();
        price = price/7;
        changePrice(price,period,this)
      }
      else
        changePrice(0,0,this)
      
    }
    else
    {
      var price = $(this).parents(".view-booking-popup").find('#monthly_price-edit').val();
     $(this).parents(".view-booking-popup").find('#apartment-price-edit').text(price);   
      
      if($(this).parents(".view-booking-popup").find('.period-months-edit').val() != "")
      {
        period = $(this).parents(".view-booking-popup").find('.period-months-edit').val();
        changePrice(price,period,this)
      }
      else
        changePrice(0,0,this)
    }
   
 
  });
  
  $('.period-days-edit').focusout(function(){
    valu = parseInt($(this).val());
    if( $(this).parents(".view-booking-popup").find(".from-miladi-edit").val() == "" )
    {
      $(this).parents(".view-booking-popup").find('.from-miladi-edit').datepicker('setDate', new Date());
    }
    
    $(this).parents(".view-booking-popup").find('.period-months-edit').val(Math.floor(valu/30));
    
    var date1 = $(this).parents(".view-booking-popup").find(' .from-miladi-edit').datepicker('getDate');
    var date = new Date( Date.parse( date1 ) ); 
    date.setDate( date.getDate() + valu );      

    var newDate = date.toDateString(); 
    newDate = new Date( Date.parse( newDate ) );
    
    $(this).parents(".view-booking-popup").find('.to-miladi-edit').datepicker('setDate', date);
   
        //change by others part
    var date1 = $(this).parents(".view-booking-popup").find('.from-miladi-edit').datepicker('getDate');
    $(this).parents('.view-booking-popup').find('.show-from-hijri-from-edit').text(writeIslamicDate(date1,1));
    $(this).parents('.view-booking-popup').find('#show-from-hijri-from-edit').val(writeIslamicDate(date1,1));
    
    var date1 = $(this).parents(".view-booking-popup").find(' .to-miladi-edit').datepicker('getDate');
    $(this).parents('.view-booking-popup').find('.show-from-hijri-to-edit').text(writeIslamicDate(date1,1));
    $(this).parents('.view-booking-popup').find('#show-from-hijri-to-edit').val(writeIslamicDate(date1,1));
    $(this).parents(".view-booking-popup").find('.apartment-row').removeAttr("disabled");
    
    if($.isNumeric($(this).parents(".view-booking-popup").find('#apartment-price-edit').text()) && $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  0)
    {
      price = $(this).parents(".view-booking-popup").find('#apartment-price-edit').text();
      changePrice(price,valu,this);
    }
    else if ($.isNumeric($(this).parents(".view-booking-popup").find(' #apartment-price-edit').text()) && $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  1)
    {
      price = $(this).parents(".view-booking-popup").find('#apartment-price-edit').text()/7;
      changePrice(price,valu,this);
    }
    
    else if ($.isNumeric($(this).parents(".view-booking-popup").find(' #apartment-price-edit').text()) && $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  2 )
    {
      price = $(this).parents(".view-booking-popup").find('#apartment-price-edit').text();
      changePrice(price,valu/30,this);
    }
    else
      changePrice(0,0,this);
  });

  $('.period-months-edit').focusout(function(){
    valu = parseInt($(this).val());
    if($(this).parents(".view-booking-popup").find(".from-miladi-edit").val() == "")
    {
      $(this).parents(".view-booking-popup").find('.from-miladi-edit').datepicker('setDate', new Date());
    }
    
    $(this).parents(".view-booking-popup").find('.period-days-edit').val(Math.floor(valu*30));
    
    var date1 =$(this).parents(".view-booking-popup").find('.from-miladi-edit').datepicker('getDate');
    var date = new Date( Date.parse( date1 ) ); 
    date.setMonth( date.getMonth()+valu);      
    var newDate = date.toDateString(); 
    newDate = new Date( Date.parse( newDate ) );
    
    $(this).parents(".view-booking-popup").find('.to-miladi-edit').datepicker('setDate', date);
    
    //change by others part
    var date1 = $(this).parents(".view-booking-popup").find('.from-miladi-edit').datepicker('getDate');
    $(this).parents('.view-booking-popup').find('.show-from-hijri-from-edit').text(writeIslamicDate(date1,1));
    $(this).parents('.view-booking-popup').find('#show-from-hijri-from-edit').val(writeIslamicDate(date1,1));
    
    var date1 = $(this).parents(".view-booking-popup").find('.to-miladi-edit').datepicker('getDate');
    $(this).parents('.view-booking-popup').find('.show-from-hijri-to-edit').text(writeIslamicDate(date1,1));
    $(this).parents('.view-booking-popup').find('#show-from-hijri-to-edit').val(writeIslamicDate(date1,1));
    $(this).parents(".view-booking-popup").find('.apartment-row').removeAttr("disabled");
    if( $.isNumeric($(this).parents(".view-booking-popup").find(' #apartment-price-edit').text())&& $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  2  )
    {
      price = $(this).parents(".view-booking-popup").find(' #apartment-price-edit').text();
      changePrice(price,valu,this);
    }
    else if( $.isNumeric($(this).parents(".view-booking-popup").find(' #apartment-price-edit').text())&& $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  0)
    {
      price = $(this).parents(".view-booking-popup").find(' #apartment-price-edit').text();
      changePrice(price,valu*30,this);
    }
    else if(  $.isNumeric(  $(this).parents(".view-booking-popup").find('#apartment-price-edit').text())&& $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  1  )
    {
      price = $(this).parents(".view-booking-popup").find('#apartment-price-edit').text();
      changePrice(price,valu*30/7,this);
    }
    
  });
  
  $('.from-miladi-edit').change(function(){
    var date1 = $(this).parents(".view-booking-popup").find('.from-miladi-edit').datepicker('getDate');
    $(this).parents('.view-booking-popup').find('.show-from-hijri-from-edit').text(writeIslamicDate(date1,1));
    $(this).parents('.view-booking-popup').find('#show-from-hijri-from-edit').val(writeIslamicDate(date1,1));
  
  });
  
  //fill hijri on miladi input-change
  $('.to-miladi-edit').change(function(){
    var date1 = $(this).parents(".view-booking-popup").find(' .to-miladi-edit').datepicker('getDate');
    $(this).parents('.view-booking-popup').find('.show-from-hijri-to-edit').text(writeIslamicDate(date1,1));
    $(this).parents('.view-booking-popup').find('#show-from-hijri-to-edit').val(writeIslamicDate(date1,1));
  
  });
  
  $(".from-miladi-edit" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      dateFormat: 'yy-mm-dd',
      numberOfMonths: 1,
      onClose: function( selectedDate ) {
        $(this).parents(".view-booking-popup").find(".to-miladi-edit" ).datepicker( "option", "minDate", selectedDate );
        start = $(this).parents(".view-booking-popup").find('.from-miladi-edit').datepicker('getDate');
        end = $(this).parents(".view-booking-popup").find('.to-miladi-edit').datepicker('getDate');
        
        if(end != null)
        {
          
          var days   = (end - start)/1000/60/60/24;
           var months   = Math.floor(days/30);
           $(this).parent().parent().find('.period-days-edit').val(days);
           $(this).parent().parent().parent().find('.period-months-edit').val(months);
           $(this).parents(".view-booking-popup").find('.apartment-row').removeAttr("disabled");
        }
        if( $.isNumeric( $(this).parents('form').find('.period-days-edit').val() ) )
        {
          period = $(this).parents('form').find('.period-days-edit').val();
          
          if( $.isNumeric($(this).parents(".view-booking-popup").find(' #apartment-price-edit').text())&& $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  2  )
          {
            price = $(this).parents(".view-booking-popup").find(' #apartment-price-edit').text();
            changePrice(price,period/30,this);
          }
          else if( $.isNumeric($(this).parents(".view-booking-popup").find(' #apartment-price-edit').text())&& $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  0)
          {
            price = $(this).parents(".view-booking-popup").find(' #apartment-price-edit').text();
            changePrice(price,period,this);
          }
          else if(  $.isNumeric(  $(this).parents(".view-booking-popup").find('#apartment-price-edit').text())&& $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  1  )
          {
            price = $(this).parents(".view-booking-popup").find('#apartment-price-edit').text();
            changePrice(price,period/7,this);
          }
        }
      }
    });
    
  $(".to-miladi-edit" ).datepicker({
       defaultDate: "+1w",
      changeMonth: true,
      dateFormat: 'yy-mm-dd',
      numberOfMonths: 1,
      onClose: function( selectedDate ) {
        $(this).parents(".view-booking-popup").find(".from-miladi-edit" ).datepicker( "option", "maxDate", selectedDate );
        start =  $(this).parents(".view-booking-popup").find('.from-miladi-edit').datepicker('getDate');
        end =  $(this).parents(".view-booking-popup").find('.to-miladi-edit').datepicker('getDate');
        if(start != null)
        {
           var days   = (end - start)/1000/60/60/24;
           var months   = Math.floor(days/30);
           $(this).parent().parent().find('.period-days-edit').val(days);
           $(this).parent().parent().parent().find('.period-months-edit').val(months);
           $(this).parents(".view-booking-popup").find('.apartment-row').removeAttr("disabled");
        }
        
        if( $.isNumeric( $(this).parents('form').find('.period-days-edit').val() ) )
        {
          period = $(this).parents('form').find('.period-days-edit').val();
          
          if( $.isNumeric($(this).parents(".view-booking-popup").find(' #apartment-price-edit').text())&& $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  2  )
          {
            price = $(this).parents(".view-booking-popup").find(' #apartment-price-edit').text();
            changePrice(price,period/30,this);
          }
          else if( $.isNumeric($(this).parents(".view-booking-popup").find(' #apartment-price-edit').text())&& $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  0)
          {
            price = $(this).parents(".view-booking-popup").find(' #apartment-price-edit').text();
            changePrice(price,period,this);
          }
          else if(  $.isNumeric(  $(this).parents(".view-booking-popup").find('#apartment-price-edit').text())&& $(this).parents(".view-booking-popup").find('.rent_type-edit').val() ==  1  )
          {
            price = $(this).parents(".view-booking-popup").find('#apartment-price-edit').text();
            changePrice(price,period/7,this);
          }
        }
      },
    });
    
     /* $('.chosen-apartment').live( "click", function() {
    var id=$(this).attr("id");
    var daily_price = $(this).parent().siblings('#daily-rent').text();
    var weekly_price = $(this).parent().siblings('#weekly-rent').text();
    var monthly_price = $(this).parent().siblings('#monthly-rent').text();
    var name = $(this).parent().siblings('#apartment_name').text();
    
    $.colorbox.close();
    
    $('#apartment-found').text(name);
    $('#apartment_id').val(id);
    $('#weekly_price').val(weekly_price);
    $('#daily_price').val(daily_price);
    $('#monthly_price').val(monthly_price);
    
    
    if($('.rent_type').val() == 0)
    {
      var price = daily_price;
    }
    else if($('.rent_type').val() == 1)
    {
      var price = weekly_price;
    }
    else
    {
      var price = monthly_price;
    } 
    
    $('#apartment-price').text(price);
    $('.company-selection').removeAttr("disabled");
    $('.client-selection').removeAttr("disabled");
    
    if($('.period-days').val() != "" && $('.rent_type').val() ==  0)
      period = $('.period-days').val();
    else if ($('.period-days').val() != "" && $('.rent_type').val() ==  1)
    {
      period = $('.period-days').val();
      price = price/7;
    }
    else
      period = $('.period-months').val();
      
    changePrice(price,period);
  });*/
  
  //choose client and effect its fields on click
   $('.chosen-client').live( "click", function() {
    var id=$(this).attr("id");
    var name=$(this).parent().parent().find('#client-name').text();
    $(this).parents(".view-booking-popup").find('#client_id-edit').val(id);
    $.colorbox.close();
    $(this).parents(".view-booking-popup").find('.client-found-edit').attr("id",id);
    $(this).parents(".view-booking-popup").find('.client-found-edit').text(name);
    $(this).parents(".view-booking-popup").find('.company-selection-edit').attr("disabled","disabled");
  });
  
  //choose company and effect its fields on click
  $('.chosen-company').live( "click", function() {
    var id=$(this).attr("id");
    var name=$(this).parent().parent().find('#company-name').text();
    $(this).parents(".view-booking-popup").find('#company_id-edit').val(id);
    $(this).parents(".view-booking-popup").find('#company_name-edit').val(name);
    var discount=parseInt($(this).parent().parent().find('#discount').text());
    $.colorbox.close();
    
    $(this).parents(".view-booking-popup").find('.company-found-edit').attr("id",id);
    $(this).parents(".view-booking-popup").find('.company-found-edit').text(name);
    $(this).parents(".view-booking-popup").find('.client-selection-edit').attr("disabled","disabled");
    $(this).parents(".view-booking-popup").find('.discount-edit').text(discount+"%");
    current = parseInt(  $(this).parents(".view-booking-popup").find('.total_price-edit').text()  ); 
    current = current - (current*(discount/100));
    $(this).parents(".view-booking-popup").find('.final_price-edit,.credit-edit').text(current);
    $(this).parents(".view-booking-popup").find('#final_price-edit').val(current);
    $(this).parents(".view-booking-popup").find('.credit-edit').text("-"+current);
  });
  
 
  

  
  $('.get-discount-edit').focusout(function(){
    var valu= $(this).parents(".view-booking-popup").find('.get-discount-edit').val();
    var current = $(this).parents(".view-booking-popup").find('.total_price-edit').text();
    valu = current - ((valu/100) * current);
    $(this).parents(".view-booking-popup").find('.final_price-edit').text(valu);
    $(this).parents(".view-booking-popup").find('#final_price-edit').val(current);
    $(this).parents(".view-booking-popup").find('.credit-edit').text("-"+valu);
  });
  
  
  $('.save-botton-3').live( "click", function() {
    if (  $(this).closest(".reciept").valid()  ) {
      var data = $(this).closest(".reciept").serialize();
      var buttonObject = $(this);
      $.ajax({
        type: "POST",
        url: "recipt_vouchers.php",
        data: data,
        dataType:"html",
        success: function(response) {
          contract_id = $(buttonObject).closest(".reciept").find('.contract_id').val();
          
          recently_paid = parseFloat($(buttonObject).closest(".reciept").find('.amout-paid').val());
          $("#edit_client"+contract_id).find('.recipets').append("<option value='"+response+"'>"+response+"# |"+recently_paid+" ر .س</option>");
          
          //update paid        
          paid = parseFloat( $("#edit_client"+contract_id).find('.what_paid').text()  );
          total_paid = paid + recently_paid;
          $("#edit_client"+contract_id).find('.what_paid').text(total_paid);
          
          //get what paid back
          paid_back = parseFloat($("#edit_client"+contract_id).find('.paid_back').text());
          
          //update credit
          final_price =  $("#edit_client"+contract_id).find('.final_price-edit').text();
          new_credit = (parseFloat(total_paid) - parseFloat(paid_back) - parseFloat(final_price));
          $("#edit_client"+contract_id).find('.credit-edit').text(new_credit);
          
          if(new_credit > 0)
            clas = "green";
          else if(new_credit < 0)
            clas = "red";
          else
            clas = "black";
                
          $("#edit_client"+contract_id).find('.credit-edit').removeClass('red green');
          $("#edit_client"+contract_id).find('.credit-edit').addClass(clas);

          if(  $(buttonObject).parents(".reciept").find('.amout-paid').is('[readonly]') )
          {
            $("#edit_client"+contract_id).find('form').submit();
          }
          else
          {
            $.magnificPopup.open({
              items: {
                src: '#edit_client'+contract_id
              },
              type: 'inline',
            });
          }
          //if(new_cerdit > 0)
            //
        }  
      });
    }
    else
    {
    }
     
  });
  
  $('.save-botton-4').live( "click", function() {
    
    if (  $(this).parents(".reciept-1").valid()  ) {
      var data = $(this).parents(".reciept-1").serialize();
      var buttonObject = $(this);
      $.ajax({
        type: "POST",
        url: "payment_vouchers.php",
        data: data,
        dataType:"html",
        success: function(response) {
          contract_id = $(buttonObject).parents(".reciept-1").find('.contract_id').val();
          recently_paid = parseFloat(  $(buttonObject).parents(".reciept-1").find('.amout-paid-1').val()  );
          
          $("#edit_client"+contract_id).find(".recipets-1").append("<option value='"+response+"'>"+response+"# |"+recently_paid+" ر .س</option>");
          
          //update what paid back
          paid_back = parseFloat(  $("#edit_client"+contract_id).find('.paid_back').text()  );
          total_paid_back = paid_back + recently_paid;
          $("#edit_client"+contract_id).find('.paid_back').text(total_paid_back);          
          
          //get what paid
          paid = parseFloat(  $("#edit_client"+contract_id).find('.what_paid').text()  );
                     
          //update the actual credit
          final_price =$("#edit_client"+contract_id).find('.final_price-edit').text();
          new_credit = (parseFloat(paid) - parseFloat(total_paid_back) - parseFloat(final_price));
          $("#edit_client"+contract_id).find('.credit-edit').text(new_credit);
                   
          if(new_credit > 0)
            clas = "green";
          else if(new_credit < 0)
            clas = "red";
          else
            clas = "black";
                
          $("#edit_client"+contract_id).find('.credit-edit').removeClass('red green');
          $("#edit_client"+contract_id).find('.credit-edit').addClass(clas);
          
          if(  $(buttonObject).parents(".reciept-1").find('.amout-paid-1').is('[readonly]') )
          {
            $("#edit_client"+contract_id).find('form').submit();
          }
          else
          {
            $.magnificPopup.open({
              items: {
                src: '#edit_client'+contract_id
              },
              type: 'inline',
            });
          }
        }  
      });
    }
    else
    {
    }
     
  });

  $(".date-miladi" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat: 'yy-mm-dd',
      onClose: function(  ) {
        var date1 = $(this).parents(".reciept").find('.date-miladi').datepicker('getDate');
        $(this).parents(".reciept").find('.hijri-date').text(writeIslamicDate(date1,1));
        $(this).parents(".reciept").find('#hijri-date').val(writeIslamicDate(date1,1));
      }
    });
    
  $(".date-1-miladi" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat: 'yy-mm-dd',
      onClose: function(  ) {
        var date1 =  $(this).parents(".reciept-1").find('.date-1-miladi').datepicker('getDate');
          
        $(this).parents(".reciept-1").find('.hijri-date-1').text(writeIslamicDate(date1,1));
        $(this).parents(".reciept-1").find('#hijri-date-1').val(writeIslamicDate(date1,1));
      }
    });
    
  $('.check-out').click(function(){
    $(this).siblings('#what-button').val(3);    
    credit =  $(this).parents(".view-booking-popup").find('.credit-edit').text();
    if( credit == 0 )
    {
      $(this).parents('form').submit();
    }
    else if(credit > 0)
    {
      var didConfirm = confirm("للعميل مبلغ مستحق لإكمال تصفية العقد، هل تود إنشاء سند صرف بقيمة "+credit+"  ريال سعودى؟  ");
      if(didConfirm == true)
      {
        contract_id = $(this).parents("#editContract").find('.contract_id').val();
        
        $("#add_payment"+contract_id).find('#date-miladi-3').datepicker('setDate', new Date());
        $("#add_payment"+contract_id).find('.hijri-date-1').text(writeIslamicDate(new Date(),1));
        $("#add_payment"+contract_id).find('#hijri-date-1').val(writeIslamicDate(new Date(),1));
        $("#add_payment"+contract_id).find('.date-miladi-3').attr('readonly', true);
        $("#add_payment"+contract_id).find('.amout-paid-1').val(Math.abs(credit));
        $("#add_payment"+contract_id).find('.amout-paid-1').attr('readonly', true);
       
        $.magnificPopup.open({
            items: {
              src: '#add_payment'+contract_id
            },
            type: 'inline',
          });
      }
      else
      {
        //do nothing
      }
    }
    else
    {
      var didConfirm = confirm("لم يتم تحصيل المبلغ المطلوب لإكمال التكلفة النهائية، هل تود إنشاء سند قبض بالمبلغ المتبقى "+Math.abs(credit)+ " ريال سعودى؟");
      if(didConfirm == true)
      {
        $(this).parents(".view-booking-popup").find('.date-miladi').datepicker('setDate', new Date());
        $(this).parents(".view-booking-popup").find('.hijri-date').text(writeIslamicDate(new Date(),1));
        $(this).parents(".view-booking-popup").find('#hijri-date').val(writeIslamicDate(new Date(),1));
        $(this).parents(".view-booking-popup").find('.date-miladi').attr('readonly', true);
        $(this).parents(".view-booking-popup").find('.amout-paid').val(Math.abs(credit));
        $(this).parents(".view-booking-popup").find('.amout-paid').attr('readonly', true);
        contract_id = $(this).parents(".view-booking-popup").find('.contract_id').val();
        $.magnificPopup.open({
            items: {
              src: '#add_recipet'+contract_id
            },
            type: 'inline',
          });
          
      }
      else
      {
        //do nothing
      } 
    }
  });
  
  


});

